/*
    Copyright (C) 2009  Twinator S.A.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.twnavigator.model
{
	import com.adobe.flex.extras.controls.springgraph.Item;
	import com.twnavigator.service.TwService;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;	

	/**
	 * Represents a single Twitter item. When created, it uses the Twitter web service
	 * to find out its title, icon, and similar products.
	 * 
	 * @author Anibal Sanchez
	 */
	public class TwItem extends Item
	{
		[Bindable]
		public var twId: String;
		
		[Bindable]
		public var twName: String;
		
		[Bindable]
		public var twScreenName: String;
		
		[Bindable]
		public var twProfileImageUrl: String;				

		public function TwItem(twId: String, twScreenName: String) {
			super(twId);
			
			this.twId = twId;
			this.twScreenName = twScreenName;
			
			TwService.getItemInfo(twScreenName, this);
		}

		public function getItemInfoResult(event:ResultEvent):void {
			CursorManager.removeBusyCursor();
			try {
				var status: XMLList;	
				var contacts: XMLList;
				var resultCode: int;
				
				var info: XML = XML(event.result);
				
				resultCode = info.child("status");		
				if (resultCode != 0) throw new ArgumentError("Error Code: " + resultCode);
				
				contacts = info.child("user");			
	
				if (contacts.length() > 0) {
					var contact: XML = contacts[0];
					
					this.twId = contact.child("id");
					this.twName = contact.child("name");
					this.twScreenName = contact.child("screen_name");
					this.twProfileImageUrl = contact.child("profile_image_url");
				}	
			} catch (erObject:Error) {
				Alert.show("getItemInfoResult Exception "+erObject.toString());
			}						
		}
		
		public function getItemInfoResultFriendsList(event:ResultEvent):void {		
			try {			
				var status: XML;				
				var friendsNode: XMLList;
				var contacts: XMLList;
				var resultCode: int;
								
				var info: XML = XML(event.result);
				
				resultCode = info.child("status");		
				if (resultCode != 0) throw new ArgumentError("Error Code: " + resultCode);
				
				friendsNode = info.child("users");
				contacts = friendsNode[0].child("user");
				
				if (contacts.length() > 0) {
					var app: TwitterNavigator = TwitterNavigator(Application.application);
					app.saveState();				
					createItems(app, contacts);
				}
			} catch (erObject:Error) {
				Alert.show("getItemInfoResultFriendsList Exception "+erObject.toString());
			}				
		}		
		
		private function createItems(app: TwitterNavigator, contacts: XMLList): void {
			for(var i: int = 0; i < contacts.length(); i++) {				
				var contact: XML = contacts[i];
				
				var twId: String = contact.child("id");
				var twScreenName: String = contact.child("screen_name");
				
				app.addItem(twId, twScreenName, this);
			}			
		}

		public function getItemInfoFault(event:FaultEvent):void {
			Alert.show("getItemInfoFault " + event.toString());
		}
		
		public function createContacts(): void {
			TwService.getItemInfoFriendsList(twScreenName, this);
		}
	}
}

			//Alert.show("getItemInfoResultFriendsList " + info.length() + "," + info.child("user").length());
/*
		public function TwItem(itemId: String, twId: int) {
			super(itemId);
			
			this.twId = twId;
			
			TwService.getItemInfo(itemId, this);
		}
*/		
				
/*			
		public function TwItem(itemId: String, twId: int, twName: String, twScreenName: String, twProfileImageUrl: String) {
			super(itemId);
			
			this.twId = twId;
			this.twName = twName;
			this.twScreenName = twScreenName;
			this.twProfileImageUrl = twProfileImageUrl;
			
			TwService.getItemInfo(itemId, this);
		}
*/		
